<template>
	<view>
		<picker @change="pickerChange" @columnchange="columnchange" :range="array" range-key="name" :value="value"
			mode="multiSelector">
			<slot></slot>
		</picker>
	</view>
</template>

<script>
	export default {
		name: "selectPicker",
		data() {
			return {
				value: [0, 0],
				array: [],
				index: 0,
				lastChild: [],
			};
		},
		props: {
			level: { //级数
				type: Number,
				default: 2
			},
			areaList: {
				type: Array,
				default: []
			}
		},
		watch: {
			areaList() {
				this.init()
				// console.log(this.areaList)
			},
		},
		created() {
			this.init()
		},
		methods: {
			init() {
				let a = []
				let c = []
				this.areaList.forEach((item, idx) => {
					// console.log(item, "item")
					if (item.children&& item.children?.length != 0) {
						item.children.unshift({
							name: '全部',
							id: 'all_mendian',
						});
						a.push({
							name: item.name,
							lastChild: item.children
						})
						c.push(...item.children)
					}
				})
				a.unshift({
					name: '全部',
					id: 'all_daqu',
				});
				c.unshift({
					name: '全部',
					id: 'all_mendian',
				});
				this.lastChild = c
				this.array = [a, c]

				this.$forceUpdate()



			},
			// 初始化地址选项
			// init() {
			// 	let a = []
			// 	let c = []
			// 	this.areaList.forEach((item, idx) => {
			// 		console.log(item,"item")
			// 		if (item.children.length != 0) {
			// 			item.children.unshift({
			// 				name: '全部',
			// 				id: 'all_mendian',
			// 			});
			// 			a.push({
			// 				name: item.name,
			// 				lastChild: item.children
			// 			})
			// 			c.push(...item.children)
			// 		}
			// 	})
			// 	a.unshift({
			// 		name: '全部',
			// 		id: 'all_daqu',
			// 	});
			// 	c.unshift({
			// 		name: '全部',
			// 		id: 'all_mendian',
			// 	});
			// 	this.lastChild = c
			// 	this.array = [a, c]

			// 	this.$forceUpdate()



			// },
			// 地址控件改变控件
			columnchange(e) {
				let aIndex = JSON.parse(JSON.stringify(e.detail.column + 1)); //选择的第几列
				let j = e.detail.value; //索引值
				let newArea = this.array
				if (aIndex == 1) {
					if (j == 0) {
						this.array[1] = this.lastChild
					} else {
						this.array[1] = newArea[0][j].lastChild
					}
				}
				this.$forceUpdate();
			},
			//点击确定 
			pickerChange(e) {
				var result = [];
				for (let i = 0; i < this.array.length; i++) {
					result.push({
						name: this.array[i][e.target.value[i]].name,
						id: this.array[i][e.target.value[i]].id
					})
				};
				this.$emit('change', {
					data: result
				})
			}

		}
	}
</script>

<style>
</style>

}
</script>

<style>

</style>
